﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tosi {

    class Simulation {

        private void loadMap() {
            //------------------------------ Junctions initialization
            Var.JUNCTIONS = new Junction[Var.JncNumber];
            for (int i = 0; i < Var.JncNumber; i++)
            {
                Var.JUNCTIONS[i] = new Junction();
            }

            //------------------------------ Streets initialization
            Var.STREETS = new Street[Var.StrNumber];
            for (int i = 0; i < Var.StrNumber; i++)
            {
                Var.STREETS[i] = new Street(Var.gr.getStreet(i)[0], Var.gr.getStreet(i)[1], i);
                Var.JUNCTIONS[Var.StrArr[i, 0]].addOutStreet(ref Var.STREETS[i]);
                Var.JUNCTIONS[Var.StrArr[i, 1]].addInStreet(ref Var.STREETS[i]);
                Var.STREETS[i].setMyJunction(ref Var.JUNCTIONS[Var.StrArr[i, 1]]);
                Var.STREETS[i].myLight.setMyJunction(ref Var.JUNCTIONS[Var.StrArr[i, 1]]);
            }
            for (int i = 0; i < Var.JncNumber; i++)
            {
                Var.JUNCTIONS[i].setMaxCars();
            }
        }
        private void loadCars()
        {
            Var.CARS = new Car[Var.CarNr];
            Var.gr.setCarNr(Var.CarNr);
            for (int i = 0; i < Var.CarNr; i++) { 
                int randStreet = Var.varRandom.getRandomNr(0, Var.StrNumber-1);
                if (Var.STREETS[randStreet].cars.Count < Var.STREETS[randStreet].getMaxCars())
                {
                    Var.CARS[i] = new Car(ref Var.STREETS[randStreet], i);
                    Var.STREETS[randStreet].carIn(Var.CARS[i]);
                    Var.gr.setCar(i, Var.CARS[i].getCarPos());
                    System.Threading.Thread.Sleep(10);
                }
                else
                { 
                    bool ok = false;
                    while (ok == false) {
                        randStreet = Var.varRandom.getRandomNr(0, Var.StrNumber - 1);
                        if (Var.STREETS[randStreet].cars.Count < Var.STREETS[randStreet].getMaxCars())
                        {
                            Var.CARS[i] = new Car(ref Var.STREETS[randStreet], i);
                            Var.STREETS[randStreet].carIn(Var.CARS[i]);
                            Var.gr.setCar(i, Var.CARS[i].getCarPos());
                            System.Threading.Thread.Sleep(10);
                            ok = true;
                        }
                    }
                }
            }
        }
        /*private void updateLightPriorities() {
            for (int i = 0; i < Var.StrNumber; i++) {
                Var.STREETS[i].myLight.setStationaryCars();
                Var.STREETS[i].myLight.setStreetLoad();
                Var.STREETS[i].myLight.incTime();
                Var.STREETS[i].myLight.initPriority();
            }
        }
        private void calcLightPriorities() {
            for (int i = 0; i < Var.StrNumber; i++) {
                Var.STREETS[i].getMyLight().initPriority();
            }
        }
        private void setAllLights() {
            for (int i = 0; i < Var.StrNumber; i++) {
                Var.STREETS[i].getMyLight().setLight();
            }
        }*/
        private void setAllLights() {
            int db = 0;
            for (int i = 0; i < Var.JncNumber; i++) {
                //Console.WriteLine();
                //Console.Write("{0:D2} | ", i);
                db+= Var.JUNCTIONS[i].setLights();
            }
            //Console.Write("A lampak szam = {0}",db);
            //Console.ReadKey();
        }
        private void initLights()
        {
            for (int i = 0; i < Var.JncNumber; i++)
            {
                Var.JUNCTIONS[i].initLight();
            }
        }
        public Simulation() {
            loadMap();
            loadCars();
            Var.gr.changeToFront();
        }
        public void Start() {
            Var.On = 1;
            initLights();
            while (Var.On == 1) {                
                System.Threading.Thread.Sleep(50);
                for (int j = 0; j < Var.StrNumber; j++) {
                    for (int k = 0; k < Var.STREETS[j].cars.Count; k++) {
                        Var.STREETS[j].cars[k].moveCar();
                    }
                }
                Var.gr.clearFront();
                Var.gr.cars();
                System.Threading.Thread.Sleep(Var.Delay);
                Console.Clear();
                //updateLightPriorities();
                setAllLights();
                //limitPriorities();
            }
        }
        public void Stop() {
            Var.On = 0;
        }
    }


}
